<article id="wikiArticle">
<div></div>
<p><code><strong>fill() </strong>方法将</code>类型化数组中的从起始索引到终止索引内的全部元素。这个方法的算法和 <a href="Reference/Global_Objects/Array/fill" title="fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。"><code>Array.prototype.fill()</code></a> 相同。 <em>TypedArray</em> 是这里的<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#TypedArray_objects">类型化数组类型</a>之一。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code><var>typedarray</var>.<code>fill(<var>value</var>[, <var>start<var> = 0[, <var>end</var> = this.length]])</var></var></code></code></code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>value</code></dt>
<dd>用来填充类型化数组元素的值。</dd>
<dt><code>start</code></dt>
<dd>可选参数。起始索引，默认值为 0。</dd>
<dt><code>end</code></dt>
<dd>可选参数。终止索引（填充范围不包含此索引），默认值为 <code>this.length</code>。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>修改后的类型化数组。</p>
<h2 id="描述">描述</h2>
<p>将被元素填充的区间是 [<code>start</code>, <code>end</code>)。</p>
<p><strong><code>fill </code></strong>方法接受三个参数 <code>value</code>，<code>start</code> 以及 <code>end。</code><code>start</code> 和 <code>end</code> 参数是可选的，默认值分别为 <code>0</code> 和 <code>this.length。</code></p>
<p>如果 <code>start</code> 参数是负值，它会被视为 <code>length+start</code>，其中 <code>length</code> 是类型化数组的长度。如果 <code>end</code> 参数是负值，它会被视为 <code>length+end。</code></p>
<h2 id="示例">示例</h2>
<pre><code  class="language-javascript">new Uint8Array([1, 2, 3]).fill(4);         // Uint8Array [4, 4, 4]
new Uint8Array([1, 2, 3]).fill(4, 1);      // Uint8Array [1, 4, 4]
new Uint8Array([1, 2, 3]).fill(4, 1, 2);   // Uint8Array [1, 4, 3]
new Uint8Array([1, 2, 3]).fill(4, 1, 1);   // Uint8Array [1, 2, 3]
new Uint8Array([1, 2, 3]).fill(4, -3, -2); // Uint8Array [4, 2, 3]
</code></pre>
<h2 id="兼容实现">兼容实现</h2>
<p>由于并没有一个名为 <em>TypedArray</em> 的全局变量，我们必须“按需添加”兼容实现。请配合<a href="Reference/Global_Objects/Array/fill" title="fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。"><code>Array.prototype.fill()</code></a>的兼容实现使用以下的“兼容实现”</p>
<pre><code  class="language-javascript">// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.fill
if (!Uint8Array.prototype.fill) {
  Uint8Array.prototype.fill = Array.prototype.fill;
}
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范</th>
<th scope="col">状态</th>
<th scope="col">注释</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.fill" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">TypedArray.prototype.fill</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>最初定义。</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.fill" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">TypedArray.prototype.fill</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>特性</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>基础支持</td>
<td>45</td>
<td><a href="/en-US/Firefox/Releases/37" title="Released on 2015-04-07.">37</a> (37)</td>
<td><span style="color: #f00;">未实现</span></td>
<td>32</td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>特性</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>基础支持</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><a href="/en-US/Firefox/Releases/37" title="Released on 2015-04-07.">37</a> (37)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关">相关</h2>
<ul>
<li><a href="Reference/Global_Objects/Array/fill" title="fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。"><code>Array.prototype.fill()</code></a></li>
</ul>
</article>